#include "win32LibConnect.h"
#include "neiderra/core/Core.h"
#include "neiderra/core/io/IOStream.h"
#include "neiderra/utils/Logger.h"

#include <windows.h>

using namespace neiderra::core;
using std::string;

win32Library::win32Library(Core* core): iLibrary(core)
{
}

void win32Library::connect(Core* core){
}

bool win32Library::load(const std::string& filename)
{
    libHandle_ = LoadLibrary( filename.c_str());

    if (libHandle_ == NULL) {
        getCore()->getLogger()->log(string("can't find ") + filename);
        return false;
    }

    getCore()->getLogger()->log(string("lib loaded: ")+filename);

    return true;
}

void win32Library::connect(){

}

void win32Library::disconnect(Core* core)
{
}

void* win32Library::getProcAddress(const std::string& procname)
{
    if (libHandle_ == NULL){
        core->getLogger()->fatal("lib not initialized");
        return 0;
    }

    void *pp = (void*)GetProcAddress(libHandle_, procname.c_str());

    if (pp == NULL)
    {
        core->getLogger()->fatal((string("lib doesn't contain this function: ")+procname).c_str());
        return 0;
    }

    return pp;
}

void win32Library::disconnect()
{
    FreeLibrary(libHandle_);
}

win32Library::~win32Library()
{
}
